/*
 * Copyright (c) 2018. cangoonline.net All Rights Reserved.
 * 类名称：EnUtil.java
 * 创建人：韩伟
 * 联系方式：hanwei@cangoonline.com
 */
package com.cango.framework.base.job;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author on 2019/3/4
 * .net/java 互通
 * 原样复制业财系统提供的加解密方法
 */
public class EnUtil {
    /**
     * 加密--把加密后的byte数组先进行二进制转16进制在进行base64编码
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            throw new IllegalArgumentException("Argument sKey is null.");
        }
        if (sKey.length() != 16) {
            throw new IllegalArgumentException(
                    "Argument sKey'length is not 16.");
        }
        byte[] raw = sKey.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
        String tempStr = parseByte2HexStr(encrypted);

        com.thoughtworks.xstream.core.util.Base64Encoder encoder = new com.thoughtworks.xstream.core.util.Base64Encoder();
        return encoder.encode(tempStr.getBytes("UTF-8"));
    }

    /**
     *解密--先 进行base64解码，在进行16进制转为2进制然后再解码
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String sSrc, String sKey) throws Exception {

        if (sKey == null) {
            throw new IllegalArgumentException("499");
        }
        if (sKey.length() != 16) {
            throw new IllegalArgumentException("498");
        }

        byte[] raw = sKey.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        com.thoughtworks.xstream.core.util.Base64Encoder encoder = new com.thoughtworks.xstream.core.util.Base64Encoder();
        byte[] encrypted1 = encoder.decode(sSrc);

        String tempStr = new String(encrypted1, "utf-8");
        encrypted1 = parseHexStr2Byte(tempStr);
        byte[] original = cipher.doFinal(encrypted1);
        String originalString = new String(original, "utf-8");
        return originalString;
    }

    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if(hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static void main(String[] args) throws Exception{

//        String s = "{\"result\":\"0000\",\"msg\":\"成功\",\"data\":\"NDU4QzAxMEZFMUMyQ0IxRDdDQThFMzU3QjcxQ0E4Mjc5NDkxRjQwNEFFOENGMDgxRjZBRDY1RjU0\\nNEFFREY4QTcwMzlCQkQ3MUNFQkY3RTdGRDVCQTE1QjE2RkE1QzYyRjk3QjRCRDk3Mzc5NUUwRDEz\\nNUFCMEIyOUJGOUI0MTc0Mzc4MzI3REEwNkUzN0NGOUZDOTNFQTRGMjIwNkI1MTFGRTQ4OTY5RTNG\\nOUM0QjVFOTM1QTAwMTcwM0RFMjMx\"}";
//        JSONObject j = JSONObject.parseObject(s);
//        String data = j.getString("data");

        String key = "1234567890123456";
        // String data = "NDU4QzAxMEZFMUMyQ0IxRDdDQThFMzU3QjcxQ0E4Mjc5NDkxRjQwNEFFOENGMDgxRjZBRDY1RjU0\\nNEFFREY4QTcwMzlCQkQ3MUNFQkY3RTdGRDVCQTE1QjE2RkE1QzYyRjk3QjRCRDk3Mzc5NUUwRDEz\\nNUFCMEIyOUJGOUI0MTc0Mzc4MzI3REEwNkUzN0NGOUZDOTNFQTRGMjIwNkI1MTFGRTQ4OTY5RTNG\\nOUM0QjVFOTM1QTAwMTcwM0RFMjMx";

    }
}
